<!--
 * @FilePath: App.vue
 * @Author: 杜芬
 * @Date: 2024-06-04 12:51:29
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-06-04 14:09:30
 * Copyright: 2024 xxxTech CO.,LTD. All Rights Reserved.
 * @Descripttion: 页面
-->


<script setup>
import {ref,onMounted} from'vue';
import Edit from './components/Edit.vue';
import axios from 'axios';

// TODO: 列表渲染
// 思路：声明响应式list => 调用接口获取数据 =》后端数据赋值给list =》绑定到table 组件
 const  list = ref([])
 const getList = async () => {
  //  调用接口
  const res = await axios.get('/list')
  // console.log(res)
  list.value = res.data
 }
 onMounted(() => getList())
// TODO: 删除功能
// 思路：获取当前行的id =》通过id调用删除接口 =》更新最近的列表
const onDelete =async (id) => {
  // console.log(id)
  // 调用删除接口
 await axios.delete(`/del/${id}`)
 getList()
}

// TODO: 编辑功能
// 打开弹框=》 回填数据 =》更新数据
// 1.打开弹框（获取子组件的实例，调用方法修改属性）
const editRef = ref(null)
const onEdit=(row) => {
  editRef.value.open(row)
}
// 2. 回调数据（调用详情接口 / 当前行的静态数据）


</script>

<template>
  <div class="app">
    <el-table :data="list">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name" width="150"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{row}">
          <el-button type="primary" @click="onEdit(row)" link>编辑</el-button>
          <el-button type="danger" @click="onDelete(row.id)" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <Edit ref="editRef" @on-update="getList" />
</template>

<style scoped>
.app {
  width: 980px;
  margin: 100px auto 0;
}
</style>
